<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加电影类型</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <div id="test"></div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>

</div>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-module/tableSelect/tableSelect.js"></script>
<script type="text/html" id="zizeng">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '${path}/types/all',
            height: 685,
            cellMinWidth:80,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '电影类型',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field:'tid', width:100, title: '序号',templet:'#zizeng',align: "center"},
                {field: 'typeName', width: 120, title: '电影类型', align: "center",sort:true},
                {field:'opera',title: '操作', width:200, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done:function (){

            }
        });


        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加电影类型',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['400px', '200px'],
                    content: 'movietype_add.jsp',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                let index = layer.open({
                    title: '编辑电影信息',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['400px', '200px'],
                    content: 'movietype_edit.jsp',
                    success:function(layero, index) {
                        let iframe = window['layui-layer-iframe' + index];
                        iframe.child(data);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                })
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除该电影类型吗', function (index) {;
                    $.post('${path}/types/deleteType',{"typeName":data.typeName},function (result){
                        if(result.code=="200"){
                            layer.msg('删除成功！', {icon:1});
                            table.reload("currentTableId",{
                                page:{
                                    curr: $(".layui-laypage-em").next().html()
                                }
                            });
                        }else {
                            layer.msg('删除失败！', {icon:2});
                        }
                    })
                    layer.close(index);
                });
            }
        });


        //监听单元格编辑
        table.on('edit(currentTableFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
        });
    });


</script>
<script type="text/html" id="imgTemp">
    <div><img id="image" src="http://localhost:80/movies/{{d.movieImg}}"/></div>
</script>
</body>
</html>